using System;
using System.Data;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Web;
namespace HKB.Common
{
	public class FileIO
	{
		private static FileIO _instance = null;
		public static FileIO instance
		{
			get
			{
				if (FileIO._instance == null)
				{
					FileIO._instance = new FileIO();
				}
				return FileIO._instance;
			}
		}
		public bool FileExists(string FilePath)
		{
			return System.IO.File.Exists(FilePath);
		}
		public void CreateFile(string FilePathName)
		{
			try
			{
				FilePathName = FilePathName.Replace("/", "\\");
				if (FilePathName.LastIndexOf("\\") > 0)
				{
					this.CreateFolder(FilePathName.Remove(FilePathName.LastIndexOf("\\")));
				}
				System.IO.FileInfo fileInfo = new System.IO.FileInfo(FilePathName);
				if (!fileInfo.Exists)
				{
					System.IO.FileStream fileStream = fileInfo.Create();
					fileStream.Close();
				}
			}
			catch (System.Exception ex)
			{
				throw new System.Exception(ex.Message.ToString());
			}
		}
		public void CreateFile(string FilePathName, string WriteWord, System.Text.Encoding textcode)
		{
			try
			{
				this.CreateFile(FilePathName);
				using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(FilePathName, false, textcode))
				{
					streamWriter.Write(WriteWord);
					streamWriter.Close();
				}
			}
			catch (System.Exception ex)
			{
				throw new System.Exception(ex.Message.ToString());
			}
		}
		public void CreateFile(string FilePathName, string WriteWord, System.Text.Encoding textcode, bool isCover)
		{
			try
			{
				if (isCover)
				{
					this.CreateFile(FilePathName);
				}
				else
				{
					this.CreateFile(FilePathName, isCover);
				}
				using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(FilePathName, false, textcode))
				{
					streamWriter.Write(WriteWord);
					streamWriter.Close();
				}
			}
			catch (System.Exception ex)
			{
				throw new System.Exception(ex.Message.ToString());
			}
		}
		public void CreateFile(string FilePathName, bool isCover)
		{
			try
			{
				FilePathName = FilePathName.Replace("/", "\\");
				if (FilePathName.LastIndexOf("\\") > 0)
				{
					this.CreateFolder(FilePathName.Remove(FilePathName.LastIndexOf("\\")));
				}
				System.IO.FileInfo fileInfo = new System.IO.FileInfo(FilePathName);
				if (isCover)
				{
					System.IO.FileStream fileStream = fileInfo.Create();
					fileStream.Close();
				}
				else
				{
					if (fileInfo.Exists)
					{
						throw new System.Exception("文件已存在！");
					}
					System.IO.FileStream fileStream2 = fileInfo.Create();
					fileStream2.Close();
				}
			}
			catch (System.Exception ex)
			{
				throw new System.Exception(ex.Message.ToString());
			}
		}
		public bool CreateTemplateFile(string TemplateContent, string PathName)
		{
			string text = PathName;
			if (PathName.IndexOf('.') < 0)
			{
				PathName += ".html";
			}
			bool result = false;
			try
			{
				bool flag = false;
				try
				{
					System.Threading.Monitor.Enter(this, ref flag);
					this.CreateFile(text);
					System.IO.StreamWriter streamWriter2;
					System.IO.StreamWriter streamWriter = streamWriter2 = new System.IO.StreamWriter(text, false, System.Text.Encoding.Default);
					try
					{
						streamWriter.Write(TemplateContent);
						streamWriter.Flush();
						result = true;
					}
					finally
					{
						if (streamWriter2 != null)
						{
							((System.IDisposable)streamWriter2).Dispose();
						}
					}
				}
				finally
				{
					if (flag)
					{
						System.Threading.Monitor.Exit(this);
					}
				}
			}
			catch (System.Exception ex)
			{
				throw new System.Exception(ex.Message.ToString());
			}
			return result;
		}
		public void CreateFolder(string FolderPathName)
		{
			if (FolderPathName.Trim().Length > 0)
			{
				try
				{
					if (!System.IO.Directory.Exists(FolderPathName))
					{
						System.IO.Directory.CreateDirectory(FolderPathName);
					}
				}
				catch (System.Exception ex)
				{
					throw new System.Exception(ex.Message.ToString());
				}
			}
		}
		public bool UploadFile(HttpPostedFile UploadPostFile, string UploadFilePath)
		{
			this.CreateFolder(UploadFilePath.Remove(UploadFilePath.LastIndexOf("\\")));
			bool result;
			try
			{
				UploadPostFile.SaveAs(UploadFilePath);
				result = true;
			}
			catch (System.Exception ex)
			{
				throw new System.Exception(ex.Message.ToString());
			}
			return result;
		}
		public string GetUrlHtml(string Url)
		{
			System.IO.StreamReader streamReader = null;
			WebRequest webRequest = WebRequest.Create(Url);
			WebResponse response = webRequest.GetResponse();
			bool flag = false;
			string result;
			try
			{
				streamReader = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
				result = streamReader.ReadToEnd();
				flag = true;
			}
			catch (System.Exception ex)
			{
				throw new System.Exception(ex.Message.ToString());
			}
			finally
			{
				if (flag)
				{
					streamReader.Close();
				}
			}
			return result;
		}
		public string GetUrlHtml(string Url, System.Text.Encoding code)
		{
			System.IO.StreamReader streamReader = null;
			WebRequest webRequest = WebRequest.Create(Url);
			WebResponse response = webRequest.GetResponse();
			bool flag = false;
			string result;
			try
			{
				streamReader = new System.IO.StreamReader(response.GetResponseStream(), code);
				result = streamReader.ReadToEnd();
				flag = true;
			}
			catch (System.Exception ex)
			{
				throw new System.Exception(ex.Message.ToString());
			}
			finally
			{
				if (flag)
				{
					streamReader.Close();
				}
				response.Close();
			}
			return result;
		}
		public string ReadFromFile(string FilePath)
		{
			string result = "找不到文件";
			if (System.IO.File.Exists(FilePath))
			{
				System.IO.FileStream fileStream = new System.IO.FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
				System.IO.StreamReader streamReader = new System.IO.StreamReader(fileStream, System.Text.Encoding.Default);
				result = streamReader.ReadToEnd().ToString();
				streamReader.Close();
				fileStream.Close();
			}
			return result;
		}
		public void DeleteFile(string FilePathName)
		{
			try
			{
				System.IO.FileInfo fileInfo = new System.IO.FileInfo(FilePathName);
				if (fileInfo.Exists)
				{
					fileInfo.Delete();
				}
			}
			catch (System.Exception ex)
			{
				throw new System.Exception(ex.Message.ToString());
			}
		}
		public static void DelecteFiles(string ArrayFile)
		{
			string[] array = ArrayFile.Split(new char[]
			{
				','
			});
			for (int i = 0; i < array.Length; i++)
			{
				if (System.IO.File.Exists(array[i]))
				{
					System.IO.File.Delete(array[i]);
				}
			}
		}
		public static void DelecteFile(string FileLoacl)
		{
			if (System.IO.File.Exists(FileLoacl))
			{
				System.IO.File.Delete(FileLoacl);
			}
		}
		public void DeleteChildFolder(string FolderPathName)
		{
			if (FolderPathName.Trim().Length > 0)
			{
				try
				{
					if (System.IO.Directory.Exists(FolderPathName))
					{
						System.IO.Directory.Delete(FolderPathName, true);
					}
				}
				catch (System.Exception ex)
				{
					throw new System.Exception(ex.Message.ToString());
				}
			}
		}
		public bool DeleteFileFolder(string FileFolderName)
		{
			bool result;
			if (System.IO.Directory.Exists(FileFolderName))
			{
				try
				{
					System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(FileFolderName);
					if (directoryInfo.Exists)
					{
						directoryInfo.Delete(true);
					}
				}
				catch
				{
					result = false;
					return result;
				}
			}
			else
			{
				if (System.IO.File.Exists(FileFolderName))
				{
					try
					{
						System.IO.FileInfo fileInfo = new System.IO.FileInfo(FileFolderName);
						fileInfo.Delete();
					}
					catch
					{
						result = false;
						return result;
					}
				}
			}
			result = true;
			return result;
		}
		public DataTable GetDirectoryInfo(string DriectoryName)
		{
			DataTable dataTable = new DataTable();
			dataTable.Columns.Add("Name");
			dataTable.Columns.Add("Image");
			dataTable.Columns.Add("Type");
			dataTable.Columns.Add("Size");
			dataTable.Columns.Add("FileUrl");
			dataTable.Columns.Add("UpdateTime");
			System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(DriectoryName);
			if (directoryInfo.Exists)
			{
				System.IO.FileInfo[] files = directoryInfo.GetFiles();
				for (int i = 0; i < files.Length; i++)
				{
					System.IO.FileInfo fileInfo = files[i];
					DataRow dataRow = dataTable.NewRow();
					dataRow["Image"] = "<img src=\"images/license.png\"/>";
					dataRow["Name"] = fileInfo.Name;
					dataRow["Type"] = fileInfo.Extension.Replace(".", "");
					dataRow["Size"] = this.GetFileLengthWithUnit(fileInfo.Length);
					dataRow["UpdateTime"] = fileInfo.LastWriteTime.ToString();
					dataRow["FileUrl"] = this.GetWebPath(DriectoryName + "\\" + fileInfo.Name);
					dataTable.Rows.Add(dataRow);
				}
				System.IO.DirectoryInfo[] directories = directoryInfo.GetDirectories();
				for (int i = 0; i < directories.Length; i++)
				{
					System.IO.DirectoryInfo directoryInfo2 = directories[i];
					DataRow dataRow = dataTable.NewRow();
					dataRow["Image"] = "<img src=\"images/folder_icon.gif\"/>";
					dataRow["Name"] = directoryInfo2.Name;
					dataRow["Type"] = "文件夹";
					long directoryLength = this.GetDirectoryLength(DriectoryName + "\\" + directoryInfo2.Name);
					dataRow["Size"] = this.GetFileLengthWithUnit(directoryLength);
					dataRow["UpdateTime"] = directoryInfo2.LastWriteTime.ToString();
					dataRow["FileUrl"] = this.GetWebPath(DriectoryName + "\\" + directoryInfo2.Name);
					dataTable.Rows.Add(dataRow);
				}
			}
			return dataTable;
		}
		public long GetDirectoryLength(string dirPath)
		{
			long num = 0L;
			long result;
			if (!System.IO.Directory.Exists(dirPath))
			{
				result = num;
			}
			else
			{
				System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(dirPath);
				System.IO.FileInfo[] files = directoryInfo.GetFiles();
				for (int i = 0; i < files.Length; i++)
				{
					System.IO.FileInfo fileInfo = files[i];
					num += fileInfo.Length;
				}
				System.IO.DirectoryInfo[] directories = directoryInfo.GetDirectories();
				if (directories.Length > 0)
				{
					for (int j = 0; j < directories.Length; j++)
					{
						num += this.GetDirectoryLength(directories[j].FullName);
					}
				}
				result = num;
			}
			return result;
		}
		protected string GetFileLengthWithUnit(long FileLength)
		{
			long num = 1048576L;
			long num2 = 1024L;
			string result;
			if (FileLength > num)
			{
				result = (FileLength / num).ToString() + "MB";
			}
			else
			{
				if (FileLength > num2)
				{
					result = (FileLength / num2).ToString() + "KB";
				}
				else
				{
					result = FileLength.ToString() + "B";
				}
			}
			return result;
		}
		public string GetWebPath(string path)
		{
			path = path.Replace("/", "\\");
			string oldValue = HttpContext.Current.Server.MapPath("~/");
			path = "/" + path.Replace(oldValue, "");
			path = path.Replace("\\", "/");
			path = path.Replace("//", "/");
			return path;
		}
		public string GetTruePath(string path)
		{
			string text = HttpContext.Current.Server.MapPath("~/");
			text += path.Replace("/", "\\");
			return text.Replace("\\\\", "\\");
		}
		public bool ReName(string NewPathName, string OldPathName)
		{
			bool result;
			if (System.IO.Directory.Exists(OldPathName))
			{
				try
				{
					System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(OldPathName);
					if (directoryInfo.Exists)
					{
						directoryInfo.MoveTo(NewPathName);
					}
				}
				catch
				{
					result = false;
					return result;
				}
			}
			else
			{
				if (System.IO.File.Exists(OldPathName))
				{
					try
					{
						System.IO.FileInfo fileInfo = new System.IO.FileInfo(OldPathName);
						fileInfo.MoveTo(NewPathName);
					}
					catch
					{
						result = false;
						return result;
					}
				}
			}
			result = true;
			return result;
		}
		private static void CopyDirectory(System.IO.DirectoryInfo OldDirectory, System.IO.DirectoryInfo NewDirectory)
		{
			string fullName = NewDirectory.FullName;
			if (!System.IO.Directory.Exists(fullName))
			{
				System.IO.Directory.CreateDirectory(fullName);
			}
			System.IO.FileInfo[] files = OldDirectory.GetFiles();
			System.IO.FileInfo[] array = files;
			for (int i = 0; i < array.Length; i++)
			{
				System.IO.FileInfo fileInfo = array[i];
				System.IO.File.Copy(fileInfo.FullName, fullName + "\\" + fileInfo.Name, true);
			}
			System.IO.DirectoryInfo[] directories = OldDirectory.GetDirectories();
			System.IO.DirectoryInfo[] array2 = directories;
			for (int i = 0; i < array2.Length; i++)
			{
				System.IO.DirectoryInfo directoryInfo = array2[i];
				System.IO.DirectoryInfo newDirectory = new System.IO.DirectoryInfo(fullName + "\\" + directoryInfo.Name);
				FileIO.CopyDirectory(directoryInfo, newDirectory);
			}
		}
		public void CopyDirectoryFile(string OldDirectoryStr, string NewDirectoryStr)
		{
			if (System.IO.Directory.Exists(OldDirectoryStr))
			{
				System.IO.DirectoryInfo oldDirectory = new System.IO.DirectoryInfo(OldDirectoryStr);
				System.IO.DirectoryInfo newDirectory = new System.IO.DirectoryInfo(NewDirectoryStr);
				FileIO.CopyDirectory(oldDirectory, newDirectory);
			}
			else
			{
				if (System.IO.File.Exists(OldDirectoryStr))
				{
					System.IO.FileInfo fileInfo = new System.IO.FileInfo(OldDirectoryStr);
					System.IO.File.Copy(fileInfo.FullName, NewDirectoryStr, true);
				}
			}
		}
	}
}
